﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo.Web;
using Apollo.Core;
using Apollo;

namespace Apollo.Website
{
    public partial class Controls_Edit_FormItemEnum : System.Web.UI.UserControl, IFormItem
    {
        public FormItem Item { get; set; }

        public object TargetObject { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.LabelPropertyName.CssClass = "property_name";

            if (!this.IsPostBack)
            {
                this.PopulateDropDown();

                this.LabelPropertyName.Text = this.Item.Projection.DisplayName;

                if (this.TargetObject != null)
                {
                    object obj = this.Item.Projection.Apply(this.TargetObject);

                    if (obj != null)
                    {
                        string objValue = obj.ToString();

                        foreach (ListItem li in DropDownListValue.Items)
                            li.Selected = li.Value == objValue;

                        HiddenFieldValue.Value = objValue;
                    }
                }
                else {
                    string defaultValue = this.Item.Projection.EntityProperty.DefaultValue;

                    if(!string.IsNullOrEmpty(defaultValue)) {
                        object objDefaultValue = System.Enum.Parse(this.Item.Projection.PropertyType, defaultValue);
                        if(objDefaultValue != null)
                            defaultValue = objDefaultValue.ToString();
                    }
                                        

                    foreach (ListItem li in DropDownListValue.Items)
                        li.Selected = li.Value == defaultValue;

                    HiddenFieldValue.Value = defaultValue;
                }

                this.DropDownListValue.Enabled = (this.Item.Type != FormItemType.ReadOnly);
            }
        }

        protected void PopulateDropDown()
        {
            foreach (string enumValue in System.Enum.GetNames(this.Item.Projection.PropertyType))
            {
                DropDownListValue.Items.Add(new ListItem(enumValue, enumValue));
            }

            DropDownListValue.Attributes.Add("onchange", "document.getElementById('" + this.HiddenFieldValue.ClientID + "').value = this.options[this.selectedIndex].value");

            if (this.HiddenFieldValue.Value != "")
                foreach (ListItem li in DropDownListValue.Items)
                    li.Selected = li.Value == this.HiddenFieldValue.Value;
        }

        public ActionMessage Save(object obj)
        {
            if (this.Item.Type == FormItemType.ReadOnly)
                return null;
            else if (this.Item.Type == FormItemType.Required && this.HiddenFieldValue.Value == "")
            {
                LabelPropertyName.CssClass = "property_name_error";
                return new ActionMessage(this.Item.Projection.DisplayName + " can not be null", ActionMessageType.Error);
            }

            try
            {
                object value = System.Enum.Parse(this.Item.Projection.PropertyType, this.HiddenFieldValue.Value);
                this.Item.Projection.SetValue(obj, value);
            }
            catch (Exception ex)
            {
                return new ActionMessage(this.Item.Projection.DisplayName + " error: " + ex.Message, ActionMessageType.Error);
            }

            return null;
        }
    }
}